package cn.algorithms_4ed.base.demo;

import cn.algorithms_4ed.base.dataStructure.Bag;
import cn.stdlib.StdOut;

/**
 * Bag_Stats
 * <p/>
 * User: erhu
 * Date: 13-6-11
 * Time: 下午5:02
 */
public class Bag_Stats {

    public static void main(String[] args) {
        Bag<Double> numbers = new Bag<Double>();
        numbers.add(91.7);
        numbers.add(91.5);
        numbers.add(91.9);
        numbers.add(89.1);
        numbers.add(82.0);
        numbers.add(91.2);
        numbers.add(90.1);
        numbers.add(90.7);
        numbers.add(90.2);
        numbers.add(90.3);

        int n = numbers.size();
        double sum = 0.0;
        for (Double x : numbers) {
            sum += x;
        }
        double mean = sum / n;

        sum = 0.0;
        for (double x : numbers) {
            sum += (x - mean) * (x - mean);
        }

        double std = Math.sqrt(sum / (n - 1));

        // 均值
        StdOut.printf("Mean: %.2f", mean);
        // 方差
        StdOut.printf("Variance: %.2f", sum / (n - 1));
        // 标准差
        StdOut.printf("Standard Deviation: %.2f", std);
    }
}
